import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
import { environment } from '@env/environment';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { FormBuilder } from '@angular/forms';
import { ShipStatService } from '../ship-stat.service';
import { deepCopy } from '@delon/util';

@Component({
  selector: 'app-ship-stat-storage-list',
  templateUrl: './storage-list.component.html',
  styleUrls: ['./storage-list.component.less']
})
export class StorageListComponent implements OnInit {
	@Input()
	transportBillId: any;

	@Output() outPutStorageId = new EventEmitter<string>();
	storageId = '';

	pageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	pageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};

	@ViewChild('st', { static: false }) private st!: STComponent;
	columns: STColumn[] = [
		{
			title: '位置',
			index: 'positionInfo',
			width: 120
		},
		{
			title: '包装',
			index: 'packageTypeName',
			width: 60
		},
		{
			title: '剩余件吨',
			index: 'pieceTonInfo',
			width: 120
		}
	];
	dataList = [];
	totalData: any;

	constructor(
		private fb: FormBuilder,
		private shipStatService: ShipStatService,
	) {
	}

	ngOnInit(): void {
		this.loadData(this.transportBillId);
	}

	search(): any {
		this.loadData(this.transportBillId);
	}

	reset() {
		this.loadData(this.transportBillId);
	}


	loadData(billId: string): any {
		const params = {
			transportBillId: billId,
			size: this.pageInfo.size,
			current: this.pageInfo.current
		};

		this.pageInfo.loading = true;
		this.shipStatService.storagePage(params).subscribe((res: any) => {
			this.dataList = res.records;
			this.pageInfo.total = res.total;
			this.pageSetting.total = res.total;
			this.pageInfo.loading = false;
			this.shipStatService.storageTotal(params).subscribe((total: any) => {
				this.totalData = total;
			});
		});
	}

	stChange(ret: STChange) {
		if (ret.type === 'click') {
			if (ret.click.item) {
				this.storageId = ret.click.item.id;
				this.outPutStorageId.emit(this.storageId);
			}
		}
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.pageInfo.current = ret.pi;
			this.pageInfo.size = ret.ps;
			this.loadData(this.transportBillId);
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};

}
